package com.cmj.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cmj.domain.entity.User;
import com.cmj.domain.entity.UserRole;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 用户查询mapper
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    /***
     * 根据用户名查询用户
     * @param username 用户名
     * @return 用户信息
     */
    @Select("select * from sys_user where user_name = #{username}")
    User getUserByName(String username);

    /***
     * 根据用户id查询昵称
     * @param userId 用户id
     * @return 用户名
     */
    @Select("select nick_name from sys_user where id = #{userId}")
    String getNickNameById(Long userId);

    @Select("select avatar from sys_user where id = #{userId}")
    String getAvatar(Long createBy);

    /***
     * 根据用户id查询用户
     * @param userId 用户id
     * @return 用户名
     */
    @Select("select * from sys_user where id = #{userId}")
    User getUserById(Long userId);

    /***
     * 更新用户信息
     * @param user{
     *      *     "avatar":"https://sg-blog.....",
     *      *     "email":"23412332@qq.com",
     *      *     "id":"1",
     *      *     "nickName":"sg333",
     *      *     "sex":"1"
     *      * }
     */
    int updateUser(User user);

    /**
     * 查询字段是否存在
     * @param name 数据名
     * @param column 字段名
     * @return 结果
     */
    @Select("select count(*) from sys_user where ${column} = #{name}")
    int selExist(@Param("name") String name,@Param("column") String column);

    /**
     * 注册
     * @param user 用户信息
     * @return 操作结果
     */
    int addUser(User user);

    @Insert("insert into sys_user_role values(#{userId},#{roleId});")
    int addUserRole(UserRole userRole);
}
